package kotlindemo.com.kotlindemo

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.view.*

/**
 * 1、方法和变量
 * 2、类和接口
 * 3、逻辑控制
 * 4、空指针防护
 * 5、使用kotlin开发Android程序
 */
class MainActivity : AppCompatActivity() {//AppCompatActivity后面的括号作用是调用父类构造函数

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        KotlinTest.staticTest()
        var  kotlinTest=KotlinTest();
        kotlinTest.helloKotlin()
        //----------------------实例化对象--------------------------------
        //new关键字取消了
        val student = Student()
        var age = student.age
        student.doHomeWork()
        //------------------------选择语句------------------------------
        val a = 100
        val b = 10
        var bigger = if (a > b) {
            a
        } else {
            b
        }
        var bigger1 = if (a > b) a else b
        //-------------------------when关键字-----------------------------
        val result = ""
        when (result) {
            "OK" -> print("")
            "Failed" -> {

            }
            else -> {

            }
        }
        //-----------------------for循环-------------------------------
        for (i in 0 until 1) {

        }
        var list = listOf("a", "b", "c")
        for ( i in list) {
            println(i)
        }
        list.forEach {
            println(it)
        }
        //-----------------------findView-------------------------------
        val text: TextView = findViewById(R.id.text)
        text.let {
            it.text = "abc"//给TextView赋值
            it.setOnClickListener {
                Toast.makeText(this,text.text.toString(),Toast.LENGTH_SHORT).show();
            }
        }
        text.text = "Hello Kotlin!"

        vars(1, 2, 3, 4,5)
    }

    fun vars(vararg v: Int) {
        for (vt in v) {
            println(vt)
        }
    }
    fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
        Toast.makeText(this, message, duration).show()
    }
}
